package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
@Transactional
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @Override
    public boolean saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息,setmeal表保存
        this.save(setmealDto);
        //保存套餐和菜品的关联信息
        //拿到套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//        System.out.println("---------------------------------------------------------------------------");
//        System.out.println(setmealDishes);
//        System.out.println("---------------------------------------------------------------------------");
        //使用stream流转换成map形式
        List<SetmealDish> list = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
//        System.out.println("---------------------------------------------------------------------------");
//        System.out.println(list);
//        System.out.println("---------------------------------------------------------------------------");
        return setmealDishService.saveBatch(list);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @Override
    public boolean updateWithDish(SetmealDto setmealDto) {
        //1.修改套餐的基本信息
        this.updateById(setmealDto);
        //2.修改套餐和菜品的关联信息
        //2.1删除同id的信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wrapper);
        //2.2拿到要添加到数据库的信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishList = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //2.3添加到数据库
        setmealDishService.saveBatch(setmealDishList);
        return true;
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    public boolean deleteWithDish(List<Long> ids) {
        //判断是否停售
        List<Setmeal> setmeals = this.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            if(setmeal.getStatus()==1){
                throw new CustomException("含有启售的套餐,不能删除!");
            }
        }

        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(wrapper);
        return true;
    }

}
